<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>动态路由配置</title>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>
<body>
<div id="app">
    <h1>响应路由参数的变化</h1>
    <p>
        <router-link to="/user/foo">/user/foo</router-link>
        <router-link to="/user/bar">/user/bar</router-link>
    </p>
    <router-view></router-view>
</div>
<script>
    // component
    const User = {
        template: '<div>User {{$route.params.id}}</div>',
        // 监测变化
        watch: {
            '$route'(to, from) {
                // 对路有变化做出响应
                console.log(from, to)
            }
        },
        // 导航守卫
        beforeRouteUpdate(to, from, next) {
            console.log(from, to)
            next()
        }
    }

    // routes
    const routes = [
        {path: '/user/:id', component: User}
    ]

    // router
    const router = new VueRouter({
        routes
    })

    // mount to vue
    const app = new Vue({
        router
    }).$mount('#app');
</script>
</body>
</html>
